package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.UserContext;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.TravelCommendQueryObject;
import cn.wolfcode.trip.base.query.TravelQueryObject;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @Autowired
    private ITravelCommendService travelCommendService;


    /**
     * 点击游记的时候,查询游记信息,回显
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public JsonResult list(@PathVariable Long id){
        JsonResult result = new JsonResult();
        try {
            Travel travel = travelService.queryByTravelId(id);
            result.setResult(travel);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 保存游记
     * @param travel
     * @return
     */
    @PostMapping
    public JsonResult save(Travel travel){
        JsonResult result = new JsonResult();
        try {
            User currentUser = UserContext.getCurrentUser();
            //设置作者id
            travel.setAuthor(currentUser);
            travelService.save(travel);
        } catch (Exception e) {
            result.mark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 修改游记
     * @param travel
     * @return
     */
    @PutMapping("/{id}")
    public JsonResult update(Travel travel){
        JsonResult result = new JsonResult();
        travelService.update(travel);
        result.setResult(travel);//设置当前用户,传给前台
        return result;
    }

    /**
     * 查询所有游记列表(分页)
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo query(TravelQueryObject qo){
        //待发布
        qo.setState(Travel.STATE_RELEASE);
        //公开
        qo.setIsPublic(true);
        //按照创建时间正序
        qo.setOrderBy("t.releaseTime desc");
        PageInfo<?> pageInfo = travelService.queryForList(qo);
        return pageInfo;
    }
    @GetMapping("/selectBykeyword")
    public PageInfo selectByKeyword(TravelQueryObject qo){
        //待发布
        qo.setState(Travel.STATE_RELEASE);
        //公开
        qo.setIsPublic(true);
        //按照创建时间正序
        qo.setOrderBy("t.releaseTime desc");
        PageInfo<?> pageInfo = travelService.queryForSearchList(qo);
        return pageInfo;
    }


    @GetMapping("commends")
    public PageInfo query(TravelCommendQueryObject qo){
        //按照创建时间正序
        qo.setOrderBy("schedule desc");
        PageInfo<?> pageInfo = travelCommendService.queryForApp(qo);
        return pageInfo;
    }

}
